import strFunc from 'utils/objects.js'
import {
	post,
	upLoadfile
} from 'utils/requests'
import { data } from 'uview-ui/libs/mixin/mixin';

/* 微信登录，获取用户登录凭证 */
export const wxLogin = () => {
	return new Promise((resolve, reject) => {
		uni.login({
			provider: 'weixin',
			success: (res) => {
				resolve(res.code)
			},
			fail: () => {
				reject("登录凭证获取失败")
			}
		});
	})
}
/* 微信登录 获取用户登录信息 */
export const getUserProfile = () => {
	return new Promise((resolve, reject) => {
		uni.getUserProfile({
			desc: '登录',
			lang: "zh_CN",
			success: (res) => {
				const userData = strFunc.deepCopy(res);
				resolve(userData)
			},
			fail: () => reject("用户信息获取失败")
		})
	})
}

/* 农营C账号登录 */
export const login = (userCode, userData) => post({
	url: '/applet/user/login',
	json: "json",
	loading: true,
	data: {
		code: userCode, //临时登录凭证
		rawData: userData.rawData, //用户非敏感信息
		signature: userData.signature, //签名
		encryptedData: userData.encryptedData, //用户敏感信息
		iv: userData.iv //解密算法的向量
	}
})
/* 上传用户头像 */
export const uploadUserAvatar = (filePath) => upLoadfile({
	url: "/applet/file/uploadAvatar",
	filePath,
	loading: true,
	name: "file",
})
/* 更新用户头像昵称 */
export const modifyUserWxInfo = (nickName, avatarUrl) => post({
	url: '/applet/user/modifyUserViewInfo',
	json: "json",
	loading: true,
	data: {
		"nickName": nickName,
		"avatarUrl": avatarUrl
	}
})

/* 农户是否认证 */
export const isFarmerAuthentication = () => post({
	url: "/applet/mine/attestation/isFarmerAuthentication"
})

/* 实名认证 */
export const authenticationRequest = ({
	gender,
	realName,
	idCardNum,
	backImg,
	frontImg
}) => {
	return post({
		url:"/applet/mine/attestation/farmerAuthentication",
		data: {
			gender,
			realName,
			idCardNum,
			backImg,
			frontImg
		}
	})
}
